/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Modulo3;

import Modelo.DAL.Norma_Tecnica_DAO;
import Acceso.Security;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.json.JSONObject;
import org.json.JSONArray;

/**
 *
 * @author Francis
 */
public class consultarNormaTecnicaAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    private static final String FAILURE = "failure";
    private static final String[] ALLOWED = {"Administrador","Gestionar Normas Tecnicas"};

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        if (!Security.validateSession(request, ALLOWED)) {
            Security.privilegeError(response);
            return mapping.findForward("errorPriv");
        }

        String nombre = request.getParameter("nombre");
        String codigo = request.getParameter("codigo");
        String offset = request.getParameter("offset");
        String limit = request.getParameter("limit");
        String order = request.getParameter("order");

        String atributos = "nombre, descripcion, archivo_norma, codigo ";
        String from = " from norma_tecnica ";
        String select = "Select ";
        String selectCount = "Select count(*) ";
        String where = " where 1 = 1 ";
        String limite = "";


        if (nombre != null && !nombre.equals("")) {
            where += " and lower(nombre) like lower('%"+nombre+"%')";
        }

        if (codigo != null && !codigo.equals("")) {
            where += " and lower(codigo) like lower('%"+codigo+"%')";
        }

        if (limit!= null && !limit.equals("0")) {
            limite += " limit " + limit + " offset " + offset;
        }
        if (order != null && !order.equals("")) {
            select += " ( " + selectCount + from + where + " ) , " + atributos + from + where +order + limite+ ";";
        }
        else{
            select += " ( " + selectCount + from + where + " ) , " + atributos + from + where +limite+ ";";
        }
        
        response.setContentType("text/json");
        JSONArray nt = Norma_Tecnica_DAO.consultarNormaTecnica(select);
        if (nt == null) {
            return mapping.findForward(FAILURE);
        }
        JSONObject ext = new JSONObject();
        ext.put("consultarNormaTecnica", nt);
        PrintWriter pw = response.getWriter();
        pw.write(ext.toString());
        pw.flush();
        pw.close();

        return mapping.findForward(SUCCESS);
    }
}